<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreCreditCard {
	
	//constructor
	function CoreCreditCard() {
		$this->init();
	}
	
	//methods
	function init() {
		$this->_cctypes = array();
		$this->_add(1, 'American Express', '/^(34|37)\\d{13}$/', '0', 0);
		$this->_add(2, 'Diners Club', '/^(30|36|38)\\d{12}$/', '0', 0);
		$this->_add(3, 'JCB', '/^((2131|1800)\\d{11}|3[0135]\\d{14})$/', '0', 0);
		$this->_add(4, 'MasterCard', '/^5[1-5]\\d{14}$/', '1', 0);
		$this->_add(5, 'Visa', '/^4\\d{12}(\\d{3})?$/', '1', 0);
		$this->_add(6, 'Discover Card', '/^6011\\d{12}$/', '0', 0);
		$this->_add(7, 'Solo', '/^(63|67)\\d{14}(\\d{2,3})?$/', '0', 0);
		$this->_add(8, 'Switch', '/^(49|56|63|67)\\d{14}(\\d{2,3})?$/', '0', 0);
		$this->_add(9, 'Australian Bankcard', '/^5610\\d{12}$/', '0', 0);
		$this->_add(10, 'enRoute', '/^(2014|2149)\\d{11}$/', '0', 0);
		$this->_add(11, 'Laser', '/^6304\\d{12}(\\d{2,3})?$/', '0', 0);
		$this->_add(12, 'Maestro', '/^(50|56|57|58|6)/', '0', 0);
		$this->_add(13, 'Saferpay Test Card', '/^9451123100000004$/', '0', 0);
	}
	
	function _add($id, $name, $pattern, $status, $sort_order) {
		$this->_cctypes[$id] = array(
							'id' => $id,
							'name' => $name,
							'pattern' => $pattern,
							'status' => $status,
							'sort_order' => $sort_order,
								);
	}
	
	//validate
	function validate($cc_number) {
		foreach($this->_cctypes as $k => $type) {
			if( preg_match($type['pattern'], $cc_number) ) {
				$type['cc_number'] = $cc_number;
				$type['number'] = $cc_number;
				
				return $type;
			}
		}
		
		return false;
	}
	
	function checkValidate($cc_number) {
		if( $this->validate($cc_number) == false ) {
			return false;
		} else {
			return true;
		}
	}
	
	function generateCC($type = 'Visa') {
		$cc_number = array();
		$cc_length = 16;
		
		$start = 0;
		if( $type == 'Visa' ) {
			$cc_number[$start++] = 4;
		} elseif( $type == 'Discover' ) {
			$cc_number[$start++] = 6;
			$cc_number[$start++] = 0;
			$cc_number[$start++] = 1;
			$cc_number[$start++] = 1;
		} elseif( $type == 'MasterCard' ) {
			$cc_number[$start++] = 5;
			$cc_number[$start++] = floor(rand() * 5)  + 1;
		} elseif( $type == 'Amex' || $type == 'American Express' ) {
			$cc_number[$start++] = 3;
			$cc_number[$start++] = round(rand()) ? 7 : 4;
			$cc_length = 15;
		}
		
		for($i=$start; $i<($cc_length - 1); $i++) {
			$cc_number[$i] = floor(rand() * 10);
		}
		
		$sum = 0;
		for($i=0; $i<($cc_length - 1); $i++) {
			$d = $cc_number[$i];
			if( ($i & 1) == ($cc_length & 1) ) {
				$d *= 2;
			}
			if( $d > 9 ) {
				$d -= 9;
			}
			$sum += $d;
		}
		
		//check digit
		$cd = array(0, 9, 8, 7, 6, 5, 4, 3, 2, 1);
		$cc_number[$cc_length - 1] = $cd[$sum % 10];
		
		return implode('', $cc_number);
	}	
}
//
?>